import timber.geometry.Point;
import timber.geometry.Size;

/**
	

	@author Chandler McWilliams
	@version 2006-08-31
*/
class timber.geometry.Rect
{
	private var bounds;

// ===========================================================
// - CONSTRUCTOR
// ===========================================================
	public function Rect(aleft, atop, aright, abottom) {
		bounds = {};
		bounds.xMin = aleft;
		bounds.xMax = aright;
		bounds.yMin = atop;
		bounds.yMax = abottom;		
	}
	
	// alt constructor using MC as source
	public static function rectWithMovieClip(mc):Rect {
		var mcbounds = mc.getBounds();
		return new Rect(mcbounds.xMin,
			             mcbounds.yMin,
			             mcbounds.xMax,
			             mcbounds.yMax);
	}
		
	
// ===========================================================
// - ACCESSORS
// ===========================================================
	public function get left():Number 	{ return bounds.xMin; }
	public function get right():Number 	{ return bounds.xMax; }
	public function get top():Number 	{ return bounds.yMin; }
	public function get bottom():Number { return bounds.yMax; }

	public function get xMin():Number { return bounds.xMin; }
	public function get xMax():Number { return bounds.xMax; }
	public function get yMin():Number { return bounds.yMin; }
	public function get yMax():Number { return bounds.yMax; }

	public function get x():Number { return bounds.xMin; }
	public function get y():Number { return bounds.yMin; }

	public function get width():Number	{ return Math.abs(bounds.xMax - bounds.xMin); }
	public function get height():Number	{ return Math.abs(bounds.yMax - bounds.yMin); }
	public function get size():Size	{ return new Size(width, height); }

	public function get topRight():Point		{ return new Point(right, top); }
	public function get bottomRight():Point	{ return new Point(right, bottom); }
	public function get topLeft():Point			{ return new Point(left, top); }
	public function get bottomLeft():Point		{ return new Point(left, bottom); }
	


// ===========================================================
// - UTILS
// ===========================================================
	public function intersects(aGeom):Boolean {
		switch (true) {
			case (aGeom instanceof Point):
				return aGeom.inRect(this);
				
			case (aGeom instanceof Rect):
				if (topRight.inRect(aGeom))	return true;
				if (bottomRight.inRect(aGeom))return true;
				if (topLeft.inRect(aGeom))		return true;
				if (bottomLeft.inRect(aGeom))	return true;
				return false;
			
			// TODO: MORE SHAPES
			default:
				throw new Error("Unknown geometry for intersection");
		}
			
	}


// ===========================================================
// - STATIC METHODS
// ===========================================================	
	public static function pointInRect(aPoint, aRect):Boolean {
		return aPoint.inRect(aRect);
	}

	public static function intersectsRect(aGeom, aRect):Boolean {
		return aRect.intersects(aGeom);
	}

	
	function toString() {
		return '[Rect] xMin: '+bounds.xMin+', xMax: '+bounds.xMax+', yMin: '+bounds.yMin+', yMax: '+bounds.yMax;
	}
}
